package com.itheima.bigevents.exception;

import com.itheima.bigevents.pojo.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice // 全局异常处理类
public class GloableExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e ) {
        e.printStackTrace();
        return Result.error(StringUtils.hasLength(e.getMessage()) ?e.getMessage():"操作失败");

    }

//    @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
//    public ResponseEntity<?> handleException(HttpMediaTypeNotAcceptableException ex) {
//        // 构造错误响应
//        return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body("No acceptable representation");
//    }

}

